demos.Examcontrol = Ext.extend( Ext.TabPanel, {
    html: '<div><img src="resources/img/sencha.png" width="210" height="291" /><h1>Welcome to Sencha Touch</h1><p>This is a comprehensive collection of our examples in an <br /> easy-to-navigate format. Each sample has a *view source* button which dynamically displays its associated code.<br /><br /><span>Sencha Touch (' + Ext.version +')</span></p></div>',
    align: 'center',
    id: 'examcontrol',
    name: 'examcontrol',
	tabBar: {
        dock: 'bottom',
        ui: 'dark',
		layout: {
            pack: 'center'
        },
        onTabTap: function(tab){
            var ui = sink.Main.ui;
//    		Ext.Msg.alert('Tapped', 'Tab tapped!  "'+tab.card.id+'"', Ext.emptyFn)
//        	demos.Tabcontrol.setActiveItem(demos.Patientscard)
//        	demos.Tabcontrol.currentCard(demos.Patientscard)
            this.activeTab = tab;
            if (tab.card.id == 'cchpi') {
            	ui.addHxButton.show();
            } else {
            	ui.addHxButton.hide();            	
            }
            if (tab.card.id == 'mdm') {
            	ui.addMxButton.show();
            } else {
            	ui.addMxButton.hide();            	
            }
			if (tab.card.id == 'ros' && this.cardLayout.getActiveItem().id == 'ros') console.debug('tapped 2');
            if (this.cardLayout) {
                this.cardLayout.setActiveItem(tab.card);
            }
            this.fireEvent('change', this, tab, tab.card);
        }
	},
	
/*    dockedItems: [
        {	xtype: 'toolbar',
	        dock: 'top',
	        title: 'Patient',
	        centered: true,
			items: [{
		                text: 'back',
		                ui: 'back',
		                handler: function() {
		                    if (Ext.is.Phone) demos.Tabcontrol.tabBar.setVisible(true);
		                    sink.Main.ui.setActiveItem(demos.Maincontrol, {type: 'slide', direction: 'right'});
		                    sink.Main.ui.currentCard = demos.Maincontrol;
		//                    demos.Tabcontrol.tabBar.setActiveItem('patients', {type: 'slide', direction: 'right'})
		                }
		            }, {
		                xtype: 'spacer'
		            }, {
		                text: 'save',
		                ui: 'confirm',		
		                handler: function() {
		                    var patientbadge = demos.patientscard.tab;
		                    badgeNumber = parseInt(patientbadge.badgeText);
		                    nextNumber = isNaN(badgeNumber) ? '' : badgeNumber-1;
		                    patientbadge.setBadge(nextNumber);
		//KR                            ROSpanel.btnCount = 0;;
		//                    Ext.getCmp('const1').setBadge('');
		//KR                          if (ToolbarDemo.isPhone()) ToolbarDemo.views.viewport.tabBar.setVisible(true);
		//KR                          ToolbarDemo.views.viewport.remove(ToolbarDemo.views.viewport.getActiveItem());
		//KR                          ToolbarDemo.views.viewport.setActiveItem('patients', {type: 'slide', direction: 'right'})
	                }
            }]
		}
    ],
*/
	
    cardSwitchAnimation: {
        type: 'slide',
        cover: true
    },
    defaults: {
        scroll: 'vertical'
    },
    initComponent: function() {
    	var cchpicard = new demos.CCHPI;
    	var roscard = new demos.ROS;
    	var labscard = new demos.Labs;
    	var pecard = new demos.PE;
    	var mdmcard = new demos.MDM;

        Ext.apply(this, {
	    	items: [
		    	cchpicard,
		        roscard,
				labscard,
				pecard,
				mdmcard
        	],
		    scope: this,
        });
                
        this.addListener({
    	    rosbutton: function() {
    	    	var systems = 0;
    	    	var CirROS;
    	    	var CirROSbadge = sink.Main.ui.items.items[2].dockedItems.items[0].items.items[1];
    	    	var ROSitems = roscard.items;
    	    	
    	    	for (var i=0;i<ROSitems.length;i++) {
    	    		if (ROSitems.items[i].bitWise > 0) systems++;
     	    	};
     	    	
    	    	if (systems>9) {
    	    		CirROS = 'Comp';
    	    	} else if (systems>1) {
    	    		CirROS = 'Ext';
    	    	} else if (systems==0) {
    	    		CirROS = '';
    	    	} else CirROS = 'PP';
    	    	
    	        CirROSbadge.setBadge(CirROS);
    	    },
    	    
		    hpichange: function() {
		    	var hpielements = 0;
		    	var cchpitemp = Ext.getCmp('cchpi');
		    	var CirHPI;
		    	var CirHPIbadge = sink.Main.ui.items.items[2].dockedItems.items[0].items.items[0];
		    	
		    	for (var i=0;i<cchpitemp.hpicount;i++) {
		//	    	console.debug(cchpitemp.items.items[i+1])
		//	    	console.debug('#'+i+'. severe='+cchpitemp.items.items[i+1].severe)
		    		if (cchpitemp.items.items[i+1].severe && cchpitemp.items.items[i+1].startValue != '') hpielements++;
		    	};
		//    	console.debug('Els='+hpielements+', Items='+cchpitemp.items.length+', hpicount='+cchpitemp.hpicount)
		    	
		//	    	console.debug(roscard.dockedItems.items[0].bitWise);
		//	    	console.debug(systems);
		    	if (hpielements>2 || cchpitemp.hpicount>3) CirHPI = 'Ext'
		    	else CirHPI = 'Brief';
		    	
		        CirHPIbadge.setBadge(CirHPI);
		    }

        });
        
        demos.Examcontrol.superclass.initComponent.apply(this, arguments);
    }
});

Ext.reg('examcontrol', demos.Examcontrol);